/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.common.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import etcore.webapp.grouptalk.common.object.ReplyTemplate;

/**
 * An instance of ReplyTemplateDAO is a data access object used to access ReplyTemplate,
 * which contains query and update operations
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class ReplyTemplateDAO extends BaseDAO {
	/**
	 * Find ReplyTemplate instances
	 * 
	 * @param begin
	 *            start position
	 * @param length
	 *            count of instances
	 * @return ReplyTemplate instances
	 */
	@SuppressWarnings("unchecked")
	public List<ReplyTemplate> findAll(int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(ReplyTemplate.class.getSimpleName());
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List<ReplyTemplate> results = queryObject.list();

		return results;

	}
 
 
	/**
	 * Get ReplyTemplate instance by id
	 * 
	 * @param id
	 *            instance id
	 * @return ReplyTemplate instance
	 */
	public ReplyTemplate findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();

		ReplyTemplate instance = (ReplyTemplate) session.get(ReplyTemplate.class.getName(), id);

		return instance;

	}

	/**
	 * Find instances of ReplyTemplate by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return ReplyTemplate instances
	 */
	@SuppressWarnings("unchecked")
	public List<ReplyTemplate> findByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(ReplyTemplate.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append(" = ?");

		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);

		List<ReplyTemplate> results = queryObject.list();

		return results;

	}
 
	
 
	 
	/**
	 * Find instances of ReplyTemplate by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @param begin
	 *            start position
	 * @param length
	 *            count of instances
	 * @return ReplyTemplate instances
	 */
	@SuppressWarnings("unchecked")
	public List<ReplyTemplate> findByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(ReplyTemplate.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append(" = ? order by id desc");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List<ReplyTemplate> results = queryObject.list();

		return results;

	}

	@SuppressWarnings("unchecked")
	public List<ReplyTemplate> findByCommonAndTid(Integer tid,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("from ");
		sqlStringBuffer.append(ReplyTemplate.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.tid= ? or  model.tid=0 order by model.tid desc");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, tid);
		queryObject.setFirstResult(begin);
		queryObject.setMaxResults(length);
		List<ReplyTemplate> results = queryObject.list();

		return results;

	}

	/**
	 * Find the count of Attachment instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return the count of Attachment instances
	 */
	public long findSizeByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select count(*) from ");
		sqlStringBuffer.append(ReplyTemplate.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.");
		sqlStringBuffer.append(propertyName);
		sqlStringBuffer.append(" = ?");
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, value);
		Object results = queryObject.uniqueResult();
		return results == null ? 0 : ((Number) results).longValue();

	}
	public long findSize() {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select count(*) from ");
		sqlStringBuffer.append(ReplyTemplate.class.getSimpleName());
		sqlStringBuffer.append(" as model  ");
		 
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
	 
		Object results = queryObject.uniqueResult();
		return results == null ? 0 : ((Number) results).longValue();

	}
	public long findSizeByCommonAndTid( Integer tid) {
		Session session = HibernateSessionFactory.getSession();
		StringBuffer sqlStringBuffer = new StringBuffer();
		sqlStringBuffer.append("select count(*) from ");
		sqlStringBuffer.append(ReplyTemplate.class.getSimpleName());
		sqlStringBuffer.append(" as model where model.tid=0 or model.tid=?");
	 
		Query queryObject = session.createQuery(sqlStringBuffer.toString());
		queryObject.setParameter(0, tid);
		Object results = queryObject.uniqueResult();
		return results == null ? 0 : ((Number) results).longValue();

	}

}